package com.leonyr.smartipaddemo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SeekBar;

import com.leonyr.smartipaddemo.R;

public class SeekBarWidthEdit extends LinearLayout implements TextWatcher , SeekBar.OnSeekBarChangeListener {
    private EditText editText;
    private SeekBar seekBar;
    private int max;
    private int min;
    private int progress;
    private int textSize;
    private int textColor;
    private Drawable progressDrawable;
    private Drawable thumb;
    private Drawable textBackground;
    private int barMinHeight;
    private int barMaxHeight;
    public SeekBarWidthEdit(Context context) {
        super(context);
    }

    public SeekBarWidthEdit(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SeekBarWidthEdit);
        initTa(context,ta);
    }

    public SeekBarWidthEdit(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SeekBarWidthEdit);
        initTa(context,ta);
    }

    private void initTa(Context context,TypedArray ta) {
        max = ta.getInt(R.styleable.SeekBarWidthEdit_max,10);
        min = ta.getInt(R.styleable.SeekBarWidthEdit_min,1);
        progress = ta.getInt(R.styleable.SeekBarWidthEdit_progress,0);
        textSize = ta.getInt(R.styleable.SeekBarWidthEdit_textSize,12);
        textColor = ta.getColor(R.styleable.SeekBarWidthEdit_textColor,0);
        progressDrawable = ta.getDrawable(R.styleable.SeekBarWidthEdit_progressDrawable);
        thumb = ta.getDrawable(R.styleable.SeekBarWidthEdit_thumb);
        textBackground = ta.getDrawable(R.styleable.SeekBarWidthEdit_textBackground);
        barMaxHeight = ta.getInt(R.styleable.SeekBarWidthEdit_barMaxHeight,4);
        barMinHeight = ta.getInt(R.styleable.SeekBarWidthEdit_barMinHeight,4);
        ta.recycle();
        initView(context);
    }

    private void initView(Context context){
        //设置水平方向
        setHorizontalGravity(HORIZONTAL);
        editText = new EditText(context);
        seekBar = new SeekBar(context);

        LayoutParams etParams = new LayoutParams(0,LayoutParams.MATCH_PARENT);
        etParams.setMargins(10,10,10,10);
        etParams.weight = 1;
        editText.setLayoutParams(etParams);
        editText.setPadding(0,0,0,0);
        editText.setTextSize(textSize);
        editText.setTextColor(textColor);
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        editText.setGravity(Gravity.CENTER);
        editText.addTextChangedListener(this);
        editText.setBackground(textBackground);
//        editText.setText("1");

        LayoutParams sbParams = new LayoutParams(0,LayoutParams.MATCH_PARENT);
        sbParams.weight = 5;
        seekBar.setLayoutParams(sbParams);
        seekBar.setMax(max);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            seekBar.setMin(min);
        }
        seekBar.setProgress(progress);
        seekBar.setOnSeekBarChangeListener(this);
        seekBar.setProgressDrawable(progressDrawable);
        seekBar.setThumb(thumb);
        seekBar.setMinimumHeight(barMinHeight);

        addView(seekBar);
        addView(editText);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        String text=editable.toString().trim();
        if(!TextUtils.isEmpty(text)){
            int intText = Integer.parseInt(text);
            if(intText<=min){
                seekBar.setProgress(min);
            }else if(intText>=max){
                seekBar.setProgress(max);
            }else{
                seekBar.setProgress(intText);
            }
            if(onSeekBarWidthEditTextChanged!=null){
                onSeekBarWidthEditTextChanged.onTextChanged(text);
            }
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        editText.setText(String.valueOf(i));
        if(onSeekBarWidthEditProgressChanged!=null){
            onSeekBarWidthEditProgressChanged.onProgressChanged(i);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    private OnSeekBarWidthEditProgressChanged onSeekBarWidthEditProgressChanged;
    private OnSeekBarWidthEditTextChanged onSeekBarWidthEditTextChanged;

    public interface OnSeekBarWidthEditProgressChanged{
        void onProgressChanged(int progress);
    }

    public interface OnSeekBarWidthEditTextChanged{
        void onTextChanged(String text);
    }

    public void setOnSeekBarWidthEditProgressChanged(OnSeekBarWidthEditProgressChanged onSeekBarWidthEditProgressChanged) {
        this.onSeekBarWidthEditProgressChanged = onSeekBarWidthEditProgressChanged;
    }

    public void setOnSeekBarWidthEditTextChanged(OnSeekBarWidthEditTextChanged onSeekBarWidthEditTextChanged) {
        this.onSeekBarWidthEditTextChanged = onSeekBarWidthEditTextChanged;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public void setMin(int min) {
        this.min = min;
    }

    public void setProgress(int progress) {
        this.seekBar.setProgress(progress);
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public void setProgressDrawable(Drawable progressDrawable) {
        this.progressDrawable = progressDrawable;
    }

    public void setThumb(Drawable thumb) {
        this.thumb = thumb;
    }

    public void setTextBackground(Drawable textBackground) {
        this.textBackground = textBackground;
    }

    public void setBarMinHeight(int barMinHeight) {
        this.barMinHeight = barMinHeight;
    }

    public void setBarMaxHeight(int barMaxHeight) {
        this.barMaxHeight = barMaxHeight;
    }

    public void setProgress(){

    }
}
